<?php

namespace app\admin\controller\wu;

use app\common\controller\Backend;

/**
 * 環境設備管理
 *
 * @icon fa fa-circle-o
 */
class Hjdevice extends Backend
{
    
    /**
     * Hjdevice模型对象
     * @var \app\admin\model\wu\Hjdevice
     */
    protected $model = null;

    protected $noNeedRight = ['detail', 'cjjk'];
    protected $noNeedLogin = ['detail', 'cjjk'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\wu\Hjdevice;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();

            foreach ($list as &$row) {
                //最新上傳內容
                $row['zxscnr'] = "";
                $Hjdeviceloglist = \app\admin\model\wu\Hjdevicelog::where('hjdevice_id', $row['id'])->order('id', 'DESC')->limit(1)->select();
                foreach ($Hjdeviceloglist as $row1) {
                    $row['zxscnr'] = $row1->commandinfo;
                    break;
                }

            }

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    //http://wu.test/admin/wu/hjdevice/detail?ids=2
    public function detail($ids)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row)
            $this->error(__('No Results were found'));

        $this->view->assign("row", $row);

        return $this->view->fetch();
    }

    //http://wu.test/admin/wu/hjdevice/cjjk
    public function cjjk()
    {
        $this->view->assign([
            'md1list1' => [],
            'md1list2' => [],
            'md1list3' => [],
            'md1list4' => [],
            'md1list5' => [],
            'md1list6' => [],
            'md1Xlist' => [],
        ]);

        $this->view->assign([
            'md2list1' => [],
            'md2list2' => [],
            'md2list3' => [],
            'md2list4' => [],
            'md2list5' => [],
            'md2list6' => [],
            'md2Xlist' => [],
        ]);

        $this->view->assign("wdvalue", 0);
        $this->view->assign("sdvalue", 0);
        $this->view->assign("md4", []);
        $this->view->assign("md5", []);
        $this->view->assign("md6", []);
        $this->view->assign("md7", []);

        //马达异常状态 0- 正常 1-异常
        $this->view->assign("mdstate1", 0);
        $this->view->assign("mdstate2", 0);
        $this->view->assign("mdstate3", 0);
        $this->view->assign("mdstate4", 0);

        $this->view->assign("ycgj", '');
        $this->view->assign("title", "環境即時狀態");
        return $this->view->fetch();
    }



}
